﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Xml;
using Cocos2D;
using Microsoft.Phone.Controls;
using Microsoft.Xna;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using TestTiger.Screens;


namespace TestTiger
{
    public partial class MainPage : PhoneApplicationPage
    {
         SLXnaGameComponents.GameComponentCollection components = new SLXnaGameComponents.GameComponentCollection();

        public ContentManager ContentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        //public static GamePage Instance;

        private AppDelegate app;
        public static TigerScreen SceneGame;
        public static bool NeedRefreshCoin = false;
        public MainPage()
        {
            InitializeComponent();

            // 从应用程序中获取内容管理器
            ContentManager = (Application.Current as App).Content;



            // 为此页面创建计时器
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
            //Instance = this;

            app = new AppDelegate(this, SharedGraphicsDeviceManager.Current);
            components.Add(app);
            app.Content = ContentManager;

        }

        private Texture2D tex;
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            // 设置图形设备的共享模式以启用 XNA 呈现
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
            components.Initialize();
            // 创建可用来绘制纹理的新 SpriteBatch。
            //spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            //tex = ContentManager.Load<Texture2D>("Icons\\Actors\\1");
            // 启动计时器
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // 停止计时器
            timer.Stop();

            // 设置图形设备的共享模式以禁用 XNA 呈现
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        void GameMain_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var scene = CCDirector.SharedDirector.RunningScene as SceneBase;
            if (scene != null)
            {
                var quit = false;
                scene.OnBackkeyPreesed(ref quit);
                e.Cancel = !quit;
            }
        }

        private int updateCount = 0;
        private double updateTick = 0;
        /// <summary>
        /// 允许页面运行逻辑，如更新环境、
        /// 检查冲突、收集输入和播放音频。
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {   
            var gameTime = new GameTime(e.TotalTime, e.ElapsedTime);
            components.Update(gameTime);
        }

        private int drawCount;
        /// <summary>
        /// 允许页面绘制自身。
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            var device = SharedGraphicsDeviceManager.Current.GraphicsDevice;
            device.Clear(Color.CornflowerBlue);
            var gameTime = new GameTime(e.TotalTime, e.ElapsedTime);
            components.Draw(gameTime);

            //drawCount++;
            //spriteBatch.Begin();
            //spriteBatch.Draw(tex, new Vector2(400, 200), Color.White);
            //spriteBatch.End();
        }
       
    }
}